package com.kafka.producer;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import net.sf.json.JSONObject;

import com.kafka.bean.Iccard;
import com.kafka.common.Constant;
import com.kafka.common.String.StringValues;
import com.kafka.core.BaseProducer;
import com.kafka.dao.IccardMapper;
import com.kafka.dao.UserMapper;
import com.kafka.enumtype.MsgType;
import com.kafka.enumtype.ResponseType;
import com.kafka.producer.inter.IProducer;
import com.kafka.util.BaseUtil;
import com.kafka.valid.AccountValid;

/**
 * 注册消息类
 * 
 * @author leihongan
 * 
 */
public class AccountProducer extends BaseProducer implements IProducer {
	UserMapper userDao = sqlSession.getMapper(UserMapper.class);
	IccardMapper iccardDao = sqlSession.getMapper(IccardMapper.class);
	private static PayProducer cp = new PayProducer();
	private final Producer<String, String> producer;
	public String TOPIC;

	public AccountProducer() {
		Properties properties = new Properties();
		try {
			properties.load(ClassLoader
					.getSystemResourceAsStream("account.properties"));
			TOPIC = properties.getProperty("topic");
		} catch (IOException e) {
			e.printStackTrace();
		}
		producer = new Producer<String, String>(new ProducerConfig(properties));
	}

	/**
	 * 消息处理控制类
	 */
	public String produce(JSONObject message) {
		if (MsgType.BINDACCOUNT.toString().equals(message.getString("msgtype"))){
			return bindProduce(message);
		}else if(MsgType.UNBINDACCOUNT.toString().equals(message.getString("msgtype"))){
			return unbindProduce(message);
		}else if(MsgType.LOSTCARD.toString().equals(message.getString("msgtype"))){
			return lostProduce(message);
		}else if(MsgType.ACTIVATECARD.toString().equals(message.getString("msgtype"))){
			return activateProduce(message);
		}else if(MsgType.CARDBLACKLIST.toString().equals(message.getString("msgtype"))){
			return blackListProduce(message);
		}else if (MsgType.UPDATEPAYPASSWD.toString().equals(message.getString("msgtype"))){
			return updatePayPasswd(message);
		}else {
			return null;
		}
		
	}
	
	/**
	 * 修改支付密码状态
	 * @param message
	 * @return
	 */
//	private String updateCheckPasswd(JSONObject message) {
//		log.info("###AccountProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
//		try {
//			Iccard iccard = getIccard(message);
//			iccard.setEnabled(message.getJSONObject("datas").getInt("checkpasswd"));
//			iccardDao.updateByPrimaryKey(iccard);
//			return successMsg(message.get("msgid"));
//		} catch (Exception e) {
//			return failMsg(message.get("msgid"), e.toString());
//		}
//	}

	/**
	 * 绑卡
	 * @param message
	 * @return
	 */
	private String bindProduce(JSONObject message) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("status", Constant.SUCCESS);
			params.put("message_id", message.get("msgid"));
			if(messageDao.selectByParam(params)==0){
				String msgId = message.getString("msgid");
				// 校验数据合法性
				JSONObject ck = AccountValid.validBind(rebuildJSON(message.getJSONObject("datas")), msgId);
				if (ck != null) {
					addMessage(message,TOPIC,Constant.FAIL, ck.getString("msg"));
					// 校验不通过则返回信息
					return ck.toString();
				} else {
					Iccard iccard = getIccard(message);
					// 追加appid和secretkey
					addAppidAndSecretkey(message);
					// 如果存在对应的uuid，且校验通过则绑卡
					if(iccard != null && iccard.getEnabled() != Constant.ACCOUNTUNBIND){
						message = rebuildSendJson(message,Constant.ACCOUNTACTIVATE);
						// 发送消息keyedMessage第一个参数是topic第二个参数就是发送的消息内容
						producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
						// 如果卡记录已存在，且没有被注销则重新激活卡状态
						updateCardStatus(message, Constant.ACCOUNTACTIVATE);
					} else if(iccard != null && iccard.getEnabled() == Constant.ACCOUNTUNBIND){
						return failMsg(message.get("msgid"),StringValues.ICCARD_CAN_NOT_ACTIVATE);
					} else{
						message = rebuildSendJson(message,Constant.ACCOUNTACTIVATE);
						// 发送消息keyedMessage第一个参数是topic第二个参数就是发送的消息内容
						producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
						//第一次绑卡的情况则新增记录
						addIccard(message);
					}
					log.info("###AccountProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
					addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
					return successMsg(message.get("msgid"));
				}
			}else{
				return failMsg(message.get("msgid"),StringValues.MESSAGE_SENDING_REPEAT);
			}
		} catch (Exception e) {
			addMessage(message,TOPIC,Constant.FAIL, e.toString());
			return failMsg(message.get("msgid"), e.toString());
		}
	}
	
	/**
	 * 解绑icccard
	 * @param message
	 * @return
	 */
	private String unbindProduce(JSONObject message) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("status", Constant.SUCCESS);
			params.put("message_id", message.get("msgid"));
			if(messageDao.selectByParam(params)==0){
				String msgId = message.getString("msgid");
				// 校验数据合法性
				JSONObject ck = AccountValid.validUnbind(rebuildJSON(message.getJSONObject("datas")), msgId);
				if (ck != null) {
					if (ck.get("code").equals(ResponseType.SYSTEM_ERROR.toString())
							|| ck.get("code").equals(ResponseType.DATA_FORMAT_ERROR.toString())
							|| ck.get("code").equals(ResponseType.NOT_MATCH_TYPE.toString())) {
						addMessage(message,TOPIC,Constant.FAIL, ck.getString("msg"));
						// 校验不通过则返回信息
						return ck.toString();
					} else {
						// 本地没有对应的uuid则向平台请求，并返回响应信息给客户端
						// 这里向平台发送请求，检查iccard数据是否存在
						JSONObject datas = message.getJSONObject("datas");
						JSONObject query = new JSONObject();
						query.put("cardno", datas.getString("cardno"));
						query.put("cardtype", datas.getInt("cardtype"));
						JSONObject cardInfo = rebuildJSON(queryIccardWithPlatform(query));
						// 只有跨平台解绑iccard，因此要判断是否是跨平台解绑
						if (cardInfo != null) {
							if(cardInfo.getInt("status")==Constant.ACCOUNTUNBIND){
								return failMsg(message.get("msgid"),StringValues.ICCARD_HAVE_BEEN_CLOSE);
							}
							message = rebuildSendJson(message,getMessageStatus(message));
							// 追加appid和secretkey
							addAppidAndSecretkey(message);
							// 当平台存在该用户的情况下执行平台的绑腕表/解绑腕表操作--是绑定或解绑由consumer端进行判断
							producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
							log.info("###UpdateProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
							addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
							return successMsg(message.get("msgid"));
						}else{
							// 如果平台也没有该用户返回错误信息
							return failMsg(message.get("msgid"),StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString());
						}
					}
				} else {
					Iccard iccard = getIccard(message);
					// 追加appid和secretkey
					addAppidAndSecretkey(message);
					// 如果存在对应的uuid，且校验通过则解绑
					if(iccard != null && iccard.getEnabled() == Constant.ACCOUNTUNBIND){
						// 如果卡被注销过就不需要再注销了
						return failMsg(message.get("msgid"),StringValues.ICCARD_HAVE_BEEN_CLOSE);
					}else{
						message = rebuildSendJson(message,Constant.ACCOUNTUNBIND);
						// 发送消息keyedMessage第一个参数是topic第二个参数就是发送的消息内容
						producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
						updateCardStatus(message, Constant.ACCOUNTUNBIND);
						// 更新卡中的余额
//						iccardDao.updateBalanceByCardNo(String.valueOf(0),iccard.getIccardno());
						// 给消费Producer发送指令更新平台余额
//						updatePlatformBalance(iccard);
					}
					log.info("###AccountProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
					addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
					return successMsg(message.get("msgid"));
				}
			}else{
				return failMsg(message.get("msgid"),StringValues.MESSAGE_SENDING_REPEAT);
			}
		} catch (Exception e) {
			addMessage(message,TOPIC,Constant.FAIL, e.toString());
			return failMsg(message.get("msgid"), e.toString());
		}
	}
	
//	private void updatePlatformBalance(Iccard iccard) {
//		JSONObject jsonData = new JSONObject();
//		jsonData.put("Uuid", iccard.getUuid());
//		jsonData.put("Stime", BaseUtil.getDate());
//		jsonData.put("Scount", 0);
//		jsonData.put("Opter", appID);
//		jsonData.put("Product", Constant.CLOSE_ACCOUNT_GOODSNAME);
//		jsonData.put("Id", 0);
//		jsonData.put("Sprice", 0);
//		jsonData.put("DiscountPrice", 0);
//		jsonData.put("SerialNum", "20150804"+num2);
//		jsonData.put("Sfrom", iccard.getIccardno());
//		jsonData.put("Password", iccard.getPassword());
//		jsonData.put("ptype", Constant.CLOSE_ACCOUNT_GOODSTYPE);
//		jsonData.put("CardType", iccard.getCardType());
//		jsonData.put("ShopCode", 0);
//		jsonData.put("PayDevice", "IC");
//		jsonData.put("Discount", 0);
//		jsonData.put("RealCharges", iccard.getBalance());
//		jsonData.put("CostPrice", 0);
//		jsonData.put("Pid", 0);
//
//		JSONArray jsonArray = new JSONArray();
//		jsonArray.add(jsonData);
//		
//		JSONObject json = new JSONObject();
//		json.put("msgType",MsgType.pay.toString());
//		json.put("datas", jsonArray);
//		json.put("MsgId", BaseUtil.getUUID());
//		json.put("createTime", BaseUtil.getDate());
//		
//	}

	/**
	 * 挂失处理
	 * @param message
	 * @return
	 */
	private String lostProduce(JSONObject message) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("status", Constant.SUCCESS);
			params.put("message_id", message.get("msgid"));
			if(messageDao.selectByParam(params)==0){
				String msgId = message.getString("msgid");
				// 校验数据合法性
				JSONObject ck = AccountValid.validLostCard(rebuildJSON(message.getJSONObject("datas")), msgId);
				if (ck != null) {
					if (ck.get("code").equals(ResponseType.SYSTEM_ERROR.toString())
							|| ck.get("code").equals(ResponseType.DATA_FORMAT_ERROR.toString())
							|| ck.get("code").equals(ResponseType.NOT_MATCH_TYPE.toString())) {
						addMessage(message,TOPIC,Constant.FAIL, ck.getString("msg"));
						// 校验不通过则返回信息
						return ck.toString();
					} else {
						// 本地没有对应的uuid则向平台请求，并返回响应信息给客户端
						// 这里向平台发送请求，检查iccard数据是否存在
						JSONObject datas = message.getJSONObject("datas");
						JSONObject query = new JSONObject();
						query.put("cardno", datas.getString("cardno"));
						query.put("cardtype", datas.getInt("cardtype"));
						JSONObject cardInfo = rebuildJSON(queryIccardWithPlatform(query));
						// 只有跨平台解绑iccard，因此要判断是否是跨平台解绑
						if (cardInfo != null) {
							if(cardInfo.getInt("status")==Constant.ACCOUNTUNBIND){
								return failMsg(message.get("msgid"),StringValues.ICCARD_CAN_NOT_LOST);
							}
							if(cardInfo.getInt("status")==Constant.ACCOUNTLOST){
								return failMsg(message.get("msgid"),StringValues.ICCARD_HAVE_BEEN_LOST);
							}
							message = rebuildSendJson(message,getMessageStatus(message));
							// 追加appid和secretkey
							addAppidAndSecretkey(message);
							// 当平台存在该用户的情况下执行平台的绑腕表/解绑腕表操作--是绑定或解绑由consumer端进行判断
							producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
							log.info("###UpdateProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
							addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
							return successMsg(message.get("msgid"));
						}else{
							// 如果平台也没有该用户返回错误信息
							return failMsg(message.get("msgid"),StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString());
						}
					}
				} else {
					Iccard iccard = getIccard(message);
					// 追加appid和secretkey
					addAppidAndSecretkey(message);
					// 如果存在对应的uuid，且校验通过则挂失
					if(iccard != null && iccard.getEnabled() == Constant.ACCOUNTUNBIND){
						// 如果卡已经被注销就不能再挂失
						return failMsg(message.get("msgid"),StringValues.ICCARD_CAN_NOT_LOST);
					}else{
						message = rebuildSendJson(message,Constant.ACCOUNTLOST);
						// 发送消息keyedMessage第一个参数是topic第二个参数就是发送的消息内容
						producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
						updateCardStatus(message, Constant.ACCOUNTLOST);
					}
					log.info("###AccountProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
					addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
					return successMsg(message.get("msgid"));
				}
			}else{
				return failMsg(message.get("msgid"),StringValues.MESSAGE_SENDING_REPEAT);
			}
		} catch (Exception e) {
			addMessage(message,TOPIC,Constant.FAIL, e.toString());
			return failMsg(message.get("msgid"), e.toString());
		}
	}
	
	/**
	 * 激活处理
	 * @param message
	 * @return
	 */
	private String activateProduce(JSONObject message) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("status", Constant.SUCCESS);
			params.put("message_id", message.get("msgid"));
			if(messageDao.selectByParam(params)==0){
				String msgId = message.getString("msgid");
				// 校验数据合法性
				JSONObject ck = AccountValid.validActivateCard(rebuildJSON(message.getJSONObject("datas")), msgId);
				if (ck != null) {
					if (ck.get("code").equals(ResponseType.SYSTEM_ERROR.toString())
							|| ck.get("code").equals(ResponseType.DATA_FORMAT_ERROR.toString())
							|| ck.get("code").equals(ResponseType.NOT_MATCH_TYPE.toString())) {
						addMessage(message,TOPIC,Constant.FAIL, ck.getString("msg"));
						// 校验不通过则返回信息
						return ck.toString();
					} else {
						// 本地没有对应的uuid则向平台请求，并返回响应信息给客户端
						// 这里向平台发送请求，检查iccard数据是否存在
						JSONObject datas = message.getJSONObject("datas");
						JSONObject query = new JSONObject();
						query.put("cardno", datas.getString("cardno"));
						query.put("cardtype", datas.getInt("cardtype"));
						JSONObject cardInfo = rebuildJSON(queryIccardWithPlatform(query));
						// 只有跨平台解绑iccard，因此要判断是否是跨平台解绑
						if (cardInfo != null) {
							if(cardInfo.getInt("status")==Constant.ACCOUNTUNBIND){
								return failMsg(message.get("msgid"),StringValues.ICCARD_CAN_NOT_ACTIVATE);
							}
							if(cardInfo.getInt("status")==Constant.ACCOUNTACTIVATE){
								return failMsg(message.get("msgid"),StringValues.ICCARD_HAVE_BEEN_ACTIVATE);
							}
							message = rebuildSendJson(message,getMessageStatus(message));
							// 追加appid和secretkey
							addAppidAndSecretkey(message);
							// 当平台存在该用户的情况下执行平台的绑腕表/解绑腕表操作--是绑定或解绑由consumer端进行判断
							producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
							log.info("###UpdateProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
							addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
							return successMsg(message.get("msgid"));
						}else{
							// 如果平台也没有该用户返回错误信息
							return failMsg(message.get("msgid"),StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString());
						}
					}
				} else {
					Iccard iccard = getIccard(message);
					// 追加appid和secretkey
					addAppidAndSecretkey(message);
					// 如果存在对应的uuid，且校验通过则激活
					if(iccard != null && iccard.getEnabled() == Constant.ACCOUNTUNBIND){
						// 如果卡已经被注销就不能再激活
						return failMsg(message.get("msgid"),StringValues.ICCARD_CAN_NOT_ACTIVATE);
					}else{
						message = rebuildSendJson(message,Constant.ACCOUNTACTIVATE);
						// 发送消息keyedMessage第一个参数是topic第二个参数就是发送的消息内容
						producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
						updateCardStatus(message, Constant.ACCOUNTACTIVATE);
					}
					log.info("###AccountProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
					addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
					return successMsg(message.get("msgid"));
				}
			}else{
				return failMsg(message.get("msgid"),StringValues.MESSAGE_SENDING_REPEAT);
			}
		} catch (Exception e) {
			addMessage(message,TOPIC,Constant.FAIL, e.toString());
			return failMsg(message.get("msgid"), e.toString());
		}
	}
	
	/**
	 * 黑名单处理
	 * @param message
	 * @return
	 */
	private String blackListProduce(JSONObject message) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("status", Constant.SUCCESS);
			params.put("message_id", message.get("msgid"));
			if(messageDao.selectByParam(params)==0){
				String msgId = message.getString("msgid");
				// 校验数据合法性
				JSONObject ck = AccountValid.validCardBlackList(rebuildJSON(message.getJSONObject("datas")), msgId);
				if (ck != null) {
					if (ck.get("code").equals(ResponseType.SYSTEM_ERROR.toString())
							|| ck.get("code").equals(ResponseType.DATA_FORMAT_ERROR.toString())
							|| ck.get("code").equals(ResponseType.NOT_MATCH_TYPE.toString())) {
						addMessage(message,TOPIC,Constant.FAIL, ck.getString("msg"));
						// 校验不通过则返回信息
						return ck.toString();
					} else {
						// 本地没有对应的uuid则向平台请求，并返回响应信息给客户端
						// 这里向平台发送请求，检查iccard数据是否存在
						JSONObject datas = message.getJSONObject("datas");
						JSONObject query = new JSONObject();
						query.put("cardno", datas.getString("cardno"));
						query.put("cardtype", datas.getInt("cardtype"));
						JSONObject cardInfo = rebuildJSON(queryIccardWithPlatform(query));
						// 只有跨平台解绑iccard，因此要判断是否是跨平台解绑
						if (cardInfo != null) {						
							if(cardInfo.getInt("status")==Constant.ACCOUNTBLACKLIST){
								return failMsg(message.get("msgid"),StringValues.ICCARD_CAN_NOT_BLACKLIST);
							}
							if(cardInfo.getInt("status")==Constant.ACCOUNTBLACKLIST){
								return failMsg(message.get("msgid"),StringValues.ICCARD_HAVE_BEEN_BLACKLIST);
							}
							message = rebuildSendJson(message,getMessageStatus(message));
							// 追加appid和secretkey
							addAppidAndSecretkey(message);
							// 当平台存在该用户的情况下执行平台的绑腕表/解绑腕表操作--是绑定或解绑由consumer端进行判断
							producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
							log.info("###UpdateProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
							addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
							return successMsg(message.get("msgid"));
						}else{
							// 如果平台也没有该用户返回错误信息
							return failMsg(message.get("msgid"),StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString());
						}
					}
				} else {
					Iccard iccard = getIccard(message);
					// 追加appid和secretkey
					addAppidAndSecretkey(message);
					// 如果存在对应的uuid，且校验通过则黑名单
					if(iccard != null && iccard.getEnabled() == Constant.ACCOUNTUNBIND){
						// 如果卡已经被注销就不能黑名单
						return failMsg(message.get("msgid"),StringValues.ICCARD_CAN_NOT_BLACKLIST);
					}else{
						message = rebuildSendJson(message,Constant.ACCOUNTBLACKLIST);
						// 发送消息keyedMessage第一个参数是topic第二个参数就是发送的消息内容
						producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
						updateCardStatus(message, Constant.ACCOUNTBLACKLIST);
					}
					log.info("###AccountProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
					addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
					return successMsg(message.get("msgid"));
				}
			}else{
				return failMsg(message.get("msgid"),StringValues.MESSAGE_SENDING_REPEAT);
			}
		} catch (Exception e) {
			addMessage(message,TOPIC,Constant.FAIL, e.toString());
			return failMsg(message.get("msgid"), e.toString());
		}
	}
	
	private Integer getMessageStatus(JSONObject message){
		// 判断是绑卡还是解绑
		if (MsgType.UNBINDACCOUNT.toString().equals(message.getString("msgtype"))) {
			return Constant.ACCOUNTUNBIND;
		} if (MsgType.LOSTCARD.toString().equals(message.getString("msgtype"))) {
			return Constant.ACCOUNTLOST;
		} else if (MsgType.ACTIVATECARD.toString().equals(message.getString("msgtype"))) {
			return Constant.ACCOUNTACTIVATE;
		} else if (MsgType.CARDBLACKLIST.toString().equals(message.getString("msgtype"))) {
			return Constant.ACCOUNTBLACKLIST;
		} else {
			return null;
		}
	}
	
	/**
	 * 重构发送的json数据
	 * @param message
	 * @param status
	 * @return
	 */
	private JSONObject rebuildSendJson(JSONObject message,Integer status){
		JSONObject datas = message.getJSONObject("datas");
		datas.put("status", status);
		message.put("datas", datas);
		return message;
	}

	/**
	 * 获取本地数据库iccard对象
	 * @param message
	 * @return
	 */
	private Iccard getIccard(JSONObject message) {
		Map<Object, Object> query = new HashMap<>();
		query.put("card_type", message.getJSONObject("datas").getString("cardtype"));
		query.put("iccardno", message.getJSONObject("datas").getString("cardno"));
		//查找卡是否已经绑定过，如果绑定过则更新历史记录为可用
		Iccard iccard = iccardDao.selectByCondition(query);
		return iccard;
	}

	/**
	 * json数据处理方法
	 * 
	 * @param message
	 * @return
	 */
	private void addIccard(JSONObject message) throws Exception {
		JSONObject iccardData = message.getJSONObject("datas");
		Iccard iccard = createEntity(iccardData);
		iccardDao.insert(iccard);
		sqlSession.commit();
	}

	/**
	 * 更新iccard
	 * 
	 * @param message
	 * @param status
	 * @throws Exception
	 */
	private void updateCardStatus(JSONObject message, Integer status)
			throws Exception {
		JSONObject iccardData = message.getJSONObject("datas");
		iccardDao.updateStatusByCardNo(String.valueOf(status),iccardData.getString("cardno"));
		sqlSession.commit();
	}

	/**
	 * 创建iccar实体
	 * 
	 * @param accountData
	 * @return
	 * @throws ParseException
	 */
	private Iccard createEntity(JSONObject iccardData) throws Exception {
		Iccard iccard = new Iccard();
		iccard.setUuid(iccardData.getString("uuid"));
		iccard.setConsumeCheck(iccardData.getInt("checkpasswd"));
		iccard.setCardType(Integer.valueOf(iccardData.getString("cardtype")));
		iccard.setIccardno(iccardData.getString("cardno"));
		iccard.setBalance(new BigDecimal(0));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date createTime = sdf.parse(BaseUtil.getDate());
		iccard.setCreateTime(createTime);
		iccard.setSource(iccardData.getInt("source"));
		iccard.setPassword(iccardData.getString("enpasswd"));
		iccard.setEnabled(Constant.ACCOUNTACTIVATE);
		iccard.setPerid(iccardData.getString("perid"));
		return iccard;
	}
	
	public void dcUnbindIccard(JSONObject message) throws Exception{
		Iccard iccard = getIccard(message);
		// 如果存在对应的uuid，且校验通过则解绑
		if(iccard != null && iccard.getEnabled() == Constant.ACCOUNTUNBIND){
			// 如果卡被注销过就不需要再注销了
			throw new Exception("Iccard has been bound,operation failure!");
		}else{
			updateCardStatus(message, Constant.ACCOUNTUNBIND);
		}
		message = rebuildSendJson(message,Constant.ACCOUNTUNBIND);
		// 追加appid和secretkey
		addAppidAndSecretkey(message);
		// 发送消息keyedMessage第一个参数是topic第二个参数就是发送的消息内容
		producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
		log.info("###AccountProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
		addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
	}
	
	/**
	 * 修改支付密码
	 * @param message
	 * @return
	 */
	public String updatePayPasswd(JSONObject message) {
		Map<String, Object> properties = new HashMap<>();
		properties.put("status", Constant.SUCCESS);
		properties.put("message_id", message.get("msgid"));
		if (messageDao.selectByParam(properties) == 0) {
			try {
				String msgId = message.getString("msgid");
				// 校验数据合法性
				JSONObject ck = AccountValid.validUpdatePasswd(rebuildJSON(message.getJSONObject("datas")), msgId);
				if (ck != null) {
					if (ck.get("code").equals(ResponseType.ICCARD_NOT_FOUND.toString())
							|| ck.get("code").equals(ResponseType.UUID_NOT_FOUND.toString())) {
						// 如果是找不到iccard则向平台请求查找iccard
						// 向平台请求查询该iccard号
						JSONObject datas = message.getJSONObject("datas");
						JSONObject query = new JSONObject();
						query.put("cardno", datas.getString("cardno"));
						query.put("cardtype", datas.getInt("cardtype"));
						JSONObject cardInfo = rebuildJSON(queryIccardWithPlatform(query));
						if (cardInfo != null) {
							// 如果平台存在该iccard则将消息发送给平台做跨平台处理
							message = rebuildSendJson(message,null);
							// 追加appid和secretkey
							addAppidAndSecretkey(message);
							// 当平台存在该用户的情况下将修改支付密码的请求发送给平台处理
							producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
							log.info("###UpdateProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
							addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
							return successMsg(message.get("msgid"));
						} else {
							// 如果平台也不存在这张卡则返回错误信息
							return failMsg(message.get("msgid"), StringValues.ICCARD_NOT_FOUND,ResponseType.ICCARD_NOT_FOUND.toString());
						}
					} else {
						addMessage(message, TOPIC, Constant.FAIL, ck.getString("msg"));
						return ck.toString();
					}
				} else {
					// 追加appid和secretkey
					addAppidAndSecretkey(message);
					// 如果卡在本地存在则直接在本地处理该卡
					message = rebuildSendJson(message,null);
					// 将消息同步到平台
					producer.send(new KeyedMessage<String, String>(TOPIC, message.toString()));
					// 消息发送成功后更新本地数据库
					Iccard iccard = getIccard(message);
					iccard.setPassword( message.getJSONObject("datas").getString("enpasswd"));
					iccard.setConsumeCheck(message.getJSONObject("datas").getInt("checkpasswd"));
					iccardDao.updateByPrimaryKey(iccard);
					sqlSession.commit();
					log.info("###UpdateProducer:" + message.toString() + ",time:" + BaseUtil.getDate());
					addMessage(message,TOPIC,Constant.SUCCESS, StringValues.SEND_SUCCESSFUL);
					return successMsg(message.get("msgid"));
				}
			} catch (Exception e) {
				addMessage(message, TOPIC, Constant.FAIL, e.toString());
				return failMsg(message.get("msgid"), e.toString());
			}
		} else {
			return failMsg(message.get("msgid"), StringValues.MESSAGE_SENDING_REPEAT);
		}
	}
}